package com.loadagent.core.util;

import java.nio.charset.*;
import java.nio.*;
import java.util.regex.*;
import java.util.*;

public class StrUtils {
    public static List<String> toList(final String strs) {
        final List<String> list = new ArrayList<String>();
        if (strs != null && strs.length() > 0) {
            final String[] split;
            final String[] ss = split = strs.split(",");
            for (final String s : split) {
                if (s.trim().length() > 0) {
                    list.add(s.trim());
                }
            }
        }
        return list;
    }

    public static boolean isEmpty(final String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isEmpty(final char[] str) {
        return str == null || str.length == 0;
    }

    public static boolean isNotEmpty(final String str) {
        return !isEmpty(str);
    }

    public static boolean isNotEmpty(final char[] str) {
        return !isEmpty(str);
    }

    public static char[] merger(final char[]... bts) {
        int lenght = 0;
        for (final char[] b : bts) {
            lenght += b.length;
        }
        final char[] bt = new char[lenght];
        int lastLength = 0;
        for (final char[] b2 : bts) {
            System.arraycopy(b2, 0, bt, lastLength, b2.length);
            lastLength += b2.length;
        }
        return bt;
    }

    public static char[] toChars(final byte[] bytes) {
        final byte[] bytes2 = new byte[bytes.length];
        System.arraycopy(bytes, 0, bytes2, 0, bytes.length);
        final Charset cs = Charset.forName("UTF-8");
        final ByteBuffer bb = ByteBuffer.allocate(bytes.length);
        bb.put(bytes).flip();
        final CharBuffer cb = cs.decode(bb);
        return cb.array();
    }

    public static byte[] toBytes(final char[] chars) {
        final char[] chars2 = new char[chars.length];
        System.arraycopy(chars, 0, chars2, 0, chars.length);
        final CharBuffer charBuffer = CharBuffer.wrap(chars2);
        final ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
        final byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
        Arrays.fill(charBuffer.array(), '\0');
        Arrays.fill(byteBuffer.array(), (byte) 0);
        return bytes;
    }

    public static boolean equal(final char[] char1, final char[] char2) {
        if (char1.length != char2.length) {
            return false;
        }
        for (int i = 0; i < char1.length; ++i) {
            if (char1[i] != char2[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean containsArray(final String str, final String[] array) {
        for (final String e : array) {
            if (str.contains(e)) {
                return true;
            }
        }
        return false;
    }

    public static String toCharArrayCode(final char[] chars) {
        final List<Integer> list = new ArrayList<Integer>();
        for (final char c : chars) {
            list.add((int) c);
        }
        return list.toString().replace("[", "{").replace("]", "}");
    }

    public static String insertStringArray(final String[] arrayStr, final String insertStr, final String pos) {
        final StringBuffer newStr = new StringBuffer();
        boolean isInsert = false;
        for (int i = 0; i < arrayStr.length; ++i) {
            newStr.append(arrayStr[i]).append("\r\n");
            if (arrayStr[i].startsWith(pos)) {
                newStr.append(insertStr).append("\r\n");
                isInsert = true;
            }
        }
        if (!isInsert) {
            newStr.append(insertStr).append("\r\n");
        }
        return newStr.toString();
    }

    public static boolean isMatch(final String match, final String testString) {
        final String regex = match.replaceAll("\\?", "(.?)").replaceAll("\\*+", "(.*?)");
        return Pattern.matches(regex, testString);
    }

    public static boolean isMatchs(final List<String> matches, final String testString) {
        return isMatchs(matches, testString, false);
    }

    public static boolean isMatchs(final List<String> matches, final String testString, final boolean dv) {
        if (matches == null || matches.size() == 0) {
            return dv;
        }
        for (final String m : matches) {
            if (isMatch(m, testString) || testString.startsWith(m) || testString.endsWith(m)) {
                return true;
            }
        }
        return false;
    }
}
